package com.feifei.service.impl;

import com.feifei.bean.City;
import com.feifei.bean.CityExample;
import com.feifei.mapper.CityMapper;
import com.feifei.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CityServiceImpl implements CityService {
    @Autowired
    CityMapper cityMapper;
    @Override
    public City getCityById(Integer cityId) {
        City city = cityMapper.selectByPrimaryKey(cityId);
        return city;
    }

    @Override
    public int saveCity(String cityname) {
        City city=new City();
        city.setName(cityname);
        cityMapper.insertSelective(city);
        CityExample cityExample=new CityExample();
        CityExample.Criteria criteria = cityExample.createCriteria().andNameEqualTo(cityname);
        //查询id
        List<City> cities = cityMapper.selectByExample(cityExample);
        if(cities!=null&&cities.size()!=0){
            return cities.get(0).getId();
        }
        return -1;
    }
}
